<template>
  <div class="dashboard-container">
    <div class="app-container">
    <el-card>
    <el-row>
      <el-col :span="20">
      <el-form :inline="true" ref="formRef">
      <el-form-item label="关键词">
        <el-input style="width:200px" v-model="pageList.keyword"></el-input>
      </el-form-item>
    </el-form>
    </el-col>
      <el-col :span="4" >
        <el-button style="position:absolute;right:80px" @click="clear">清除</el-button>
        <el-button type="primary" style="position:absolute;right:0px" @click="inquire">查询</el-button>
      </el-col>
    </el-row>
      <el-alert
    :closable="false"
    type="info">
   数据一共{{counts}}条
  </el-alert>
    <el-table
      :data="questionList"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
        width="190">
      </el-table-column>
      <el-table-column
        prop="questionType"
        label="题型"
        width="80">
        <template #default={row}>
          {{fn(row)}}
        </template>
      </el-table-column>
      <el-table-column
        label="题目编号"
        width="190"
        >
        <template #default={row:{questionIDs}} >
          <div  v-for="item in questionIDs" :key="item.number">
          <el-link type="primary" @click='preview(item.id)'>
            {{item.number}}
          </el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column
      prop="addTime"
      width="180"
        label="录入时间">
      </el-table-column>
      <el-table-column
      prop="totalSeconds"
        label="答题时间(s)">
      </el-table-column>
      <el-table-column
      prop="accuracyRate"
        label="正确率(%)">
      </el-table-column>
      <el-table-column
      prop="userName"
        label="录入人">
      </el-table-column>
      <el-table-column
        label="操作">
        <template #default = {row}>
      <el-button plain type="danger" icon="el-icon-delete"  size="small"  circle @click="delQuestions(row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
      <el-pagination
      style="margin-top:20px;text-align:right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageList.page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageList.pagesize"
      layout="prev, pager, next, sizes, jumper"
      :total="counts"
      background>
    </el-pagination>
    <!-- <el-dialog
     title="题目预览"
    :visible="isShowPrestion"
    width="60%"
    center
    @close = 'isShowPrestion = false'
    >
      <el-row style="border-bottom:2px solid #ccc; padding-bottom: 20px;margin-bottom:10px">
        <el-col style="padding: 10px 0" :span="6">【题型】：{{list.questionType}}</el-col>
        <el-col style="padding: 10px 0" :span="6">【编号】：{{list.id}}</el-col>
        <el-col style="padding: 10px 0" :span="6">【难度】：{{list.difficulty}}</el-col>
        <el-col style="padding: 10px 0" :span="6">【标签】：{{list.tags}}</el-col>
        <el-col style="padding: 10px 0" :span="6">【学科】：{{list.subjectID}}</el-col>
        <el-col style="padding: 10px 0" :span="6">【目录】：{{list.catalogID}}</el-col>
        <el-col style="padding: 10px 0" :span="6">【方向】：{{list.direction}}</el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isShowPrestion = false" >关 闭</el-button>
      </span>
      <div>【题干】:</div>
      <div style="margin: 20px 0" v-html="list.question"></div>
      <div>单选题 选项：（以下选中的选项为正确答案）</div>
      <div style="border-bottom:2px solid #ccc;margin-bottom:0px">
        <el-radio label="1" v-for="item in list.options" style="width:100%;padding:10px 0" :key="item.id">{{item.code}}</el-radio>
      </div>
      <div style="border-bottom:2px solid #ccc;padding:5px 0">
        【参考答案】：
        <el-button type='danger' size="small">视频答案预览</el-button>
      </div>
    <div style="border-bottom:2px solid #ccc;padding:15px 0"  >【答案解析】: <span style="display:inline-block" v-html="list.answer"></span> </div>
    <div style="margin-top:10px">【题目备注】: <a href="list.remarks">{{list.remarks}}</a> </div>
    </el-dialog> -->
    <QuestionsPreview  ref="prestionRef" ></QuestionsPreview  >
    </el-card>
  </div>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview.vue'
export default {
  components: {
    QuestionsPreview
  },
  data () {
    return {
      counts: 1, // 题库总数
      pageList: {
        pagesize: 10, // 每页条数
        page: 1, // 当前页数
        keyword: null // 关键字
      },
      questionIds: [],
      items: 10, // 列表
      questionList: []
    }
  },
  created () {
    this.getQuestionList()
  },
  methods: {
    // 请求题库列表数据
    async getQuestionList () {
      const { data } = await randoms(this.pageList)
      console.log(data)
      this.questionList = data.items
      this.counts = data.counts
    },
    // 根据数据显示题型
    fn (row) {
      if (row.questionType === '1') {
        return '单选'
      } else if (row.questionType === '2') {
        return '多选'
      } else {
        return '简答'
      }
    },
    // 当前条数发生变化的时候触发的函数
    handleSizeChange (val) {
      // console.log(val)
      this.pageList.pagesize = val
      this.getQuestionList()
    },
    // 当前页数发生变化时触发的函数
    handleCurrentChange (val) {
      // console.log(val)
      this.pageList.page = val
      this.getQuestionList()
    },
    // 清空
    clear () {
      this.pageList.keyword = null
    },
    // 删除
    async delQuestions (id) {
      // console.log(id)
      const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('已取消操作')
      await removeRandoms({ id })
      if (this.questionList.length === 1 && this.pageList.page > 1) {
        this.pageList.page--
      }
      this.getQuestionList()
    },
    // 预览
    async preview (id) {
      this.$refs.prestionRef.isShowPrestion = true
      // console.log(id)
      const { data } = await detail({ id })
      console.log(data)
      this.$refs.prestionRef.list = data
    },
    // 查询
    inquire () {
      this.pageList.page = 1
      this.getQuestionList()
    }
  }
}
</script>

<style scoped lang='less'>

</style>
